﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ZLDC.Common.Estate;
using ZLDC.Topo.BLL.Estate;
using ZLDC.Topo.Model.Estate;
using System.Web.UI.HtmlControls;
using System.Data;

namespace ZLDC.Estate.TM
{
    public partial class SavePhoto : _PageBase
    {
    
           EstateBLL ebll = new    EstateBLL();
           List<Thumbnail> thumbnails = new List<Thumbnail>();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                string type = _Request.GetString("type", "0");//字典图片类型
                string typename = _Request.GetString("typename", "");//操作类型
                thumbnails = Session["file_info"] as List<Thumbnail>;
                StringBuilder sb = new StringBuilder();
                Session["file_list"] = thumbnails;
                if (thumbnails != null)
                {
                    sb.Append("<div>");
                    sb.Append("<table id='tableList1' width='100%' height='100%' border='0' cellpadding='0' cellspacing='1' bgcolor='#cccccc'>");
                    sb.Append("<tr>"); 
                    int i = 0;
                    foreach (Thumbnail item in thumbnails)
                    {
                        string name = item.Name;
                        string pathurl = item.ImagePath;
                        int num = thumbnails.Count - 1;

                        if (i % 2 == 0 && i > 0)
                            sb.Append("</tr><tr>");

                        sb.Append("<td bgcolor='F7F7F7'><img  id='img" + num + "'  src='../" + pathurl + "'  width='230' height='242'  value='" + pathurl + "' />");
                        sb.AppendLine("<br/>描述：<select name='imgRemark'>");
                        if (typename.Equals("estate") && type.Equals("1"))
                        {
                            sb.AppendLine("<option value='小区图片'>小区图片<option/>");
                        }
                        else if (typename.Equals("housing") && type.Equals("1"))
                        {
                            sb.AppendLine("<option value='室内图'>室内图<option/>");
                        }
                        else if (type.Equals("2"))
                        {
                            sb.AppendLine("<option value='户型图'>户型图<option/>");
                        }
                        sb.AppendLine("</select><input type='text' name='inputimg' class='textstyle1' style='width:120px'  id='input" + num + "'  />");
                        sb.Append("</td>");
                        i++;

                    }
                    sb.Append("</tr>");
                    sb.Append("</table>");
                    sb.Append("</div>");
                }
                
                ltDiv.Text = sb.ToString();
            }

        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
           
            //string str = Request.Form["inputimg"].Trim();
            //获取页面上控件的值
            string strRemark = _Request.GetStringForm("imgRemark", "") ;
            string strInputimg =  _Request.GetStringForm("inputimg", "");
            string[] sArray = strRemark.Split(',');//备注
            string[] sArray2 = strInputimg.Split(',');//备注
            int id =_Convert.ToInt(Session["eid"].ToString()); //表ID
            int bid = _Request.GetInt("bid", 0);//表ID
            string ename = string.Empty;
            string bname = string.Empty;
           
            string type = Session["type"].ToString(); ;//字典图片类型
           
            string typename=Session["typename"].ToString();//操作类型
            int dltflag=0;//标记是否删除

            thumbnails = Session["file_info"] as List<Thumbnail>;
            string outmsg = "";
            if (thumbnails != null)
               {
                   if (typename == "estate")
                   {
                       if (id > 0)
                       {
                           ename = ebll.SelectNameByEid(id);//楼盘名称
                       }
                       if (bid > 0)
                       {
                           bname = BuildBLL.SelectBuildNameByBId(bid);//楼盘栋阁名称
                       }
                       //int estateid = ZLDC.Topo.BLL.Estate.HousingBLL.SelectBuildIdByHid(id);
                       if (EstatePicBLL.SavePic(thumbnails, type, id, ename, bid, bname, sArray, sArray2) > 0)
                       {
                           ClientScript.RegisterStartupScript(typeof(string), "", "<script>alert('上传图片成功！');window.close();</script>");
                       }
                   }
                   else if (typename == "housing") 
                   {
                       DataSet ds1 = ZLDC.Topo.BLL.Estate.HousingBLL.SelectBuildNameById(id);
                       string estatename = ds1.Tables[0].Rows[0]["buildName"].ToString();//楼盘栋阁名称
                       string hroomno = ds1.Tables[0].Rows[0]["roomno"].ToString();//房间号
                       int typeid = _Convert.ToInt(Session["type"].ToString());//房源图片类型
                       int i = 0;
                       foreach (var item in thumbnails)
                       {
                           outmsg = HuosingPicBLL.p_huosing_pic_insert(item.ImagePath, "", item.Name, typeid, id, estatename, hroomno, sArray[i] + sArray2[i], dltflag);

                           quantificationBLL.UpdatePhotoesQuantification();
                           i = i + 1;
                       }
                       int count=ZLDC.Topo.BLL.Estate.HuosingPicBLL.IsExitHousingPicToday(id);
                       if (count == i)
                       {
                           quantificationBLL.UpdateHasPhotoQuantification();
                       }
                       if (typeid == 2)
                       {
                           bid = _Convert.ToInt(ds1.Tables[0].Rows[0]["buildId"].ToString());//楼盘栋阁名称
                           DataTable dtBuild = BuildBLL.BuildDetail1(bid);
                           int eid = _Convert.ToInt(dtBuild.Rows[0]["eid"].ToString());
                           ename = dtBuild.Rows[0]["ename"].ToString();
                           EstatePicBLL.SavePic(thumbnails, type, eid, ename, bid, estatename, sArray, sArray2);
                       }

                       if (outmsg == "添加成功")
                       {
                           ClientScript.RegisterStartupScript(typeof(string), "", "<script>alert('上传图片成功！');window.close();</script>");
                       }
                       else
                       {
                           _Utility.GoError(outmsg);
                       }
                   }

               }
               Session["file_info"] = "";

        }
    }
}
